package com.kuangstudy.controller;

import com.kuangstudy.entity.Product;
import com.kuangstudy.service.product.IProductService;
import com.kuangstudy.utils.bean.BeanMapUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author renyitong
 * @version 1.0.0
 * @createTime 2023年09月15日
 * @Description
 */
@RestController
@Slf4j
@Api(tags = "hash商品操作")
public class ProductHashController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IProductService productService;

    @ApiOperation("保存或修改商品信息")
    @PostMapping("/create/product/info")
    public String saveProductInfo(@RequestBody Product product) {
        // 1: 将产品存入到DB数据库中
        boolean result = productService.saveProduct(product);
        // 2: 确定Hash的key
        String key = "product:hash:" + product.getId();
        // 3: 把产品信息转换成map
        Map<String, Object> productMap = BeanMapUtils.beanToMap(product);

        // 4: 将产品信息存入到hash中 putall等价于: 批量的 hmset key field1 value1 field2 value2 ....
        this.redisTemplate.opsForHash().putAll(key, productMap);

        // 5: 获取hash中的值
        Object title = this.redisTemplate.opsForHash().get(key, "name");
        Object price = this.redisTemplate.opsForHash().get(key, "price");
        Object description = this.redisTemplate.opsForHash().get(key, "description");
        log.info("从redis缓存中获取的值是：{},{},{}", title, price, description);

        // multiGet类似于：hmget key field1 field2 ...fieldn
        List<Object> list = this.redisTemplate.opsForHash().multiGet(key, Arrays.asList("name", "price", "description"));
        log.info("从redis缓存中获取的值是：{}", list);
        return "success";
    }

    /**
     * 商品价格变动
     *
     * @param productId
     * @param price  必须为整数 涨价传正数；降价勇负数
     * @return
     */
    @PostMapping("/hash/product/addprice")
    @ApiOperation("商品价格变动")
    public Object addPrice(@RequestParam("productId") Long productId, @RequestParam("price") Double price) {
        // 1: 根据商品id查询商品的价格
        Product dbProduct = productService.getById(productId);

        // 2: 按需更新
        Product product = new Product();
        product.setId(productId);
        Double dbPrice = dbProduct.getPrice();

        // 3: 进行价格的变动
        BigDecimal dbBigDecimal = new BigDecimal(dbPrice);
        BigDecimal bigDecimal = new BigDecimal(price);
        BigDecimal resBigDecimal = dbBigDecimal.add(bigDecimal);
        product.setPrice(resBigDecimal.doubleValue());

        // 4: 开始去db中修改价格
        boolean result = productService.saveProduct(product);

        // 5: 确定Hash的key
        String key = "product:hash:" + productId;
        // 6: 把产品信息转换成map 类似于：hincrbyfloat key field value
        this.redisTemplate.opsForHash().increment(key, "price", price.doubleValue());

        // 7: 获取涨价的产品价格
        Object newprice = this.redisTemplate.opsForHash().get(key, "price");
        return newprice;
    }

}
